<template>
  <div style="padding: 24px">
    <el-button type="primary"  @click="handleAdd"
      >新增</el-button
    >
    <h2>视频列表</h2>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="50"> </el-table-column>
      <el-table-column label="封面" width="180">
        <template slot-scope="scope">
          <img width="100px" :src="scope.row.videoCover" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="videoName" label="名称"> </el-table-column>
      <el-table-column prop="videoType" label="分类"> </el-table-column>
      <el-table-column prop="videoTag" label="标签"> </el-table-column>
      <el-table-column prop="remark" label="视频说明"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="添加视频" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="添加方式" :label-width="formLabelWidth">
          <el-radio-group v-model="form.radio">
            <el-radio :label=1>上传视频文件</el-radio>
            <el-radio :label=2>添加在线链接</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          v-if="form.radio == 1"
          label="选择视频"
          :label-width="formLabelWidth"
        >
          <el-upload
            class="upload-demo"
            action="http://47.242.59.183:8084/bus/outer/importFile"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleVideoSuccess"
            :file-list="form.videofile"
            list-type="video"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">上传一个视频</div>
          </el-upload>
        </el-form-item>
        <el-form-item
          label="视频链接"
          v-if="form.radio == 2"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.videoPath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="视频封面" :label-width="formLabelWidth">
          <el-upload
            class="upload-demo"
            action="http://47.242.59.183:8084/bus/outer/importFile"
            :on-preview="handlePreview"
            :on-success="handleCoverSuccess"
            :on-remove="handleRemove"
            :file-list="form.videoimg"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">上传一张封面图</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="视频名称" :label-width="formLabelWidth">
          <el-input v-model="form.videoName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="视频分类" :label-width="formLabelWidth">
          <el-select
            v-model="form.videoType"
            placeholder="请选择视频分类"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in form.videoTypeList"
              :key="index"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="视频标签" :label-width="formLabelWidth">
          <el-select
            v-model="form.videoTag"
            placeholder="请选择视频分类"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in form.videoTagList"
              :key="index"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="视频说明" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            v-model="form.videoRemark"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      limit: 10,
      page: 1,
      dialogFormVisible: false,
      form: {
        radio: 1,
        videoPath: "",
        videoCover: "",
        videoPath: "",
        videoName: "",
        videoType: "",
        videoTypeList: [],
        videoTag: "",
        videoTagList: [],
        videoRemark: "",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.getVideoList();
  },
  methods: {
    getVideoList() {
      request({
        url:
          "/bus/video/list?pageSize=" + this.limit + "&pageNum=" + this.page+"&orderByColumn=createTime&isAsc=desc",
        method: "get",
      }).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getVideoList()
    },
    handleAdd() {
      let _this = this;
      request({
        url: "/bus/outer/dictData/video_type",
        method: "get",
      }).then((res) => {
        console.log(res);
        _this.form.videoTypeList = res.data;
      });
      request({
        url: "/bus/outer/dictData/video_tag",
        method: "get",
      }).then((res) => {
        _this.form.videoTagList = res.data;
      });
      this.dialogFormVisible = true;
    },
    handleSubmit() {
      request({
        url: "/bus/video",
        method: "post",
        header:{
            "content-type":"application/x-www-form-urlencoded"
        },
        data: this.form,
      }).then((res) => {
        this.page = 1;
        this.getVideoList();
        alert("添加成功");
      });
      this.dialogFormVisible = false;
    },
    handleVideoSuccess(res, file, fileList) {
      this.form.videoPath = res.msg;
    },
    handleCoverSuccess(res, file, fileList) {
      this.form.videoCover = res.msg;
    },
    selectImg(e) {
      console.log(e);
    },
    handleDelete(e) {
      request({
        url: "/bus/video/" + e.videoId,
        method: "delete",
      }).then((res) => {
        console.log(res);
        this.page = 1;
        this.getVideoList();
        alert("删除成功");
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
  },
};
</script>